//
//  ViewController.m
//  qqqqqq
//
//  Created by iOSer on 16/5/18.
//  Copyright © 2016年 iOSer. All rights reserved.
//

#define Head_Heigth TTRealValue(230)
#define NAVBAR_CHANGE_POINT -130

#import "ViewController.h"
//#import "slidingTabsVC.h"
#import "SDCycleScrollView.h"
#import "pushViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) SDCycleScrollView *banner;
@property(nonatomic,strong) UIView *navView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    //设置tableView的contenInset,让他的contentView在230处开始显示
     self.tableView.contentInset = UIEdgeInsetsMake(Head_Heigth, 0, 0, 0);
    //设置tableView的默认偏移量
    [self.tableView setContentOffset:CGPointMake(0, -(Head_Heigth)) animated:NO];
    NSLog(@"%f",self.tableView.contentOffset.y);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    //这段直接粘的SDCycleScrollView的Demo
    NSArray *imagesURLStrings = @[
                                  @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                                  @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                                  @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd526e6d0160924c1fae5ccd60735fae7cd340d.jpg"
                                  ];
    /*
     依次是设置轮播图的url数组
     轮播图的滚动时间
     轮播图图片的显示方式,因为要实现下拉时图片显示更多 所以设置为UIViewContentModeScaleAspectFill
     */
    self.banner.imageURLStringsGroup = imagesURLStrings;
    self.banner.autoScrollTimeInterval = 2.0;
    self.banner.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
}

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


//初始化布局
-(void)setupUI{
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.banner];
    [self.view addSubview:self.navView];
    
    [_banner makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(Head_Heigth));
    }];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.bottom.equalTo(self.view);
    }];
    [_navView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(64));
    }];
}
//监听tableview滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView == self.tableView) {
        // 获取滑动偏移量(向上滑动，偏移量增大，向下滑动，偏移量减小)
        CGFloat offY = scrollView.contentOffset.y;
        CGFloat delat = offY + Head_Heigth;
        
        
        // 向上滑动，delat增大，高度减小
        CGFloat heigth = Head_Heigth - delat;
        NSLog(@"%.f:%.f:%.f:%.f",heigth,offY,delat,-(TTRealValue(230)));
        
        if (heigth < 64) {
            heigth = 64;
        }
        
        
        [self.banner updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(heigth));
        }];
        
        
        if (offY > NAVBAR_CHANGE_POINT)  {
            CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offY) / 64));
            
            [self.navView setBackgroundColor:HEXColor(0x000000, alpha)];
            
            
        } else {
            
            
            [self.navView setBackgroundColor:HEXColor(0x000000, 0)];
            
        }

    }
    
    

    
    
}
#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [UITableViewCell new];
    //完全取消cell的选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


//轮播器的代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    pushViewController *pushVC = [pushViewController new];
    pushVC.navigationItem.title = [NSString stringWithFormat:@"轮播器第%ld张图跳转",(long)index];
    [self.navigationController pushViewController:pushVC animated:YES];
    
}
//alertView代理方法
-(void)alertViewBtnClick:(NSString *)text{
    NSLog(@"提示框点击事件发生");
}
#pragma mark - 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        //设置数据源
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //隐藏滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
       
    }
    
    return _tableView;
}


-(SDCycleScrollView *)banner{
    if (!_banner) {
        _banner = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , Head_Heigth) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        _banner.currentPageDotImage = [UIImage imageNamed:@"fp_qiehuanClick"];
        _banner.pageDotImage = [UIImage imageNamed:@"fp_qiehuan"];
        
    }
    return _banner;
}

-(UIView *)navView{
    if (!_navView) {
        _navView = [UIView new];
        [_navView setBackgroundColor:[UIColor clearColor]];
    }
    return _navView;
}


@end
